package com.fangyu.lottery.strategy;

import com.fangyu.lottery.model.Prize;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 概率抽奖策略
 * @author fangyu
 * @version v1.0.0
 * @since 2025/8/27 23:40
 */
@Component("probability")
public class ProbabilityDrawStrategy implements DrawStrategy {
    @Override
    public Prize draw(List<Prize> prizes) {
        // 计算总概率
        double totalProbability = prizes.stream()
                .mapToDouble(Prize::getProbability)
                .sum();

        // 生成随机数
        double random = Math.random() * totalProbability;

        // 根据概率选择奖品
        double temp = 0;
        for (Prize prize : prizes) {
            temp += prize.getProbability();
            if (random <= temp) {
                return prize;
            }
        }

        return null;
    }
}